
package com.kensure.shike.baobei.service;


import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import co.kensure.exception.ParamUtils;
import co.kensure.frame.JSBaseService;
import co.kensure.mem.MapUtils;
import co.kensure.mem.PageInfo;

import com.kensure.basekey.BaseKeyService;
import com.kensure.shike.baobei.dao.SKTypeDao;
import com.kensure.shike.baobei.model.SKType;


/**
 * 商品类目 服务实现类
 * @author fankd created on 2019-7-9
 * @since 
 */
@Service
public class SKTypeService extends JSBaseService{
	
	@Resource
	private SKTypeDao dao;
	@Resource
	private BaseKeyService baseKeyService;
    
    public SKType selectOne(Long id){
    	return dao.selectOne(id);
    }
	
	public List<SKType> selectByIds(Collection<Long> ids){
		return dao.selectByIds(ids);
	}
	
	public List<SKType> selectAll(){
		return dao.selectAll();
	}
	
	public List<SKType> selectByWhere(Map<String, Object> parameters){
		return dao.selectByWhere(parameters);
	}
	
	
	public long selectCount(){
		return dao.selectCount();
	}
	
	public long selectCountByWhere(Map<String, Object> parameters){
		return dao.selectCountByWhere(parameters);
	}
	
	public boolean insert(SKType obj){
		super.beforeInsert(obj);
		obj.setId(baseKeyService.getKey("sk_type"));
		return dao.insert(obj);
	}	
	
	public boolean update(SKType obj){
		super.beforeUpdate(obj);
		return dao.update(obj);
	}
    
    public boolean updateByMap(Map<String, Object> params){
		return dao.updateByMap(params);
	}
    
    
	public boolean delete(Long id){
		return dao.delete(id);
	}	
	
    public boolean deleteMulti(Collection<Long> ids){
		return dao.deleteMulti(ids);
	}
    
    public boolean deleteByWhere(Map<String, Object> parameters){
		return dao.deleteByWhere(parameters);
	}
    
    
    public List<SKType> selectByPid(Long pid){
    	Map<String, Object> parameters = MapUtils.genMap("pid",pid,"orderby","disorder");
		return selectByWhere(parameters);
	}
    
    /**
     * 根据父子获取信息
     * @return
     */
	public List<SKType> getAllByTree(){
		List<SKType> list = selectByPid(0L);
		for(SKType type:list){
			type.setChilds(selectByPid(type.getId()));
		}
		return list;
	}
    
	
	/**
	 * 根据where条件查找
	 * @param typeQuery
	 * @param page
	 * @return
	 */
	public List<SKType> selectByPage(SKType typeQuery,PageInfo page) {
		Map<String, Object> parameters = MapUtils.bean2Map(typeQuery, true);
		parameters.put("orderby", "pid,disorder");
		MapUtils.putPageInfo(parameters, page);
		List<SKType> list =selectByWhere(parameters);
		for(SKType type:list){
			SKType one = selectOne(type.getPid());
			String pidname = one==null?"无":one.getName();
			type.setPidname(pidname);
		}
		return list;
	}
	
	public Long selectCountByPage(SKType typeQuery) {
		Map<String, Object> parameters = MapUtils.bean2Map(typeQuery, true);
		Long count=selectCountByWhere(parameters);
		return count;
	}
	
	public void add(SKType obj) {
		ParamUtils.isBlankThrewException(obj.getName(), "名称不能为空");
		if (obj.getId() != null) {
			update(obj);
		} else {
			insert(obj);
		}
	}
}
